import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store'

const routes = [
  // {
  //   path: '/',
  //   name: 'login',
  //   component: () => import("@/views/login/index.vue")
  // },
  {
    path: '/',
    name: 'chat',
    component: () => import("@/views/chat/index.vue"),
    async beforeEnter(_to, _from, next) {
      // 获取用户信息
      // const userResponse = await store.dispatch("user/getUser")
      // if(!userResponse) {
      //   next()
      //   return
      // }
      // 获取聊天会话
      await store.dispatch("user/login")
      const conversationResponse = await store.dispatch("conversation/getConversations", true)
      if(!conversationResponse) {
        next()
        return
      }
      store.dispatch("websocket/init")
      next()
    }
  },
  {
    path: "/give",
    name: "Give",
    component: () => import("@/views/give/index")
  },
  { path: '/:pathMatch(.*)', redirect: { name: 'chat' } }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router
